package alscript.lua.bridge.util;

import com.imy.luasupport.core.LuaCore;

import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.TwoArgFunction;

public abstract class BaseFunLib extends TwoArgFunction {

  @Override
  public LuaValue call(LuaValue arg1, LuaValue env) {
    LibRegister annotation = getClass().getAnnotation(LibRegister.class);
    if (annotation==null){
      throw  new IllegalArgumentException("please set libName");
    }
    LuaTable lib = new LuaTable();
    addFunction(lib);
    //注册
    String s = annotation.libName();
    env.set(s, lib );
    env.get("package").get("loaded").set(s, lib);
    return lib;
  }

  protected abstract void addFunction(LuaTable lib);
}
